<?php
namespace app\admin\controller;
use app\common\controller\Adminbase;
use app\admin\model\AdminUser;
use think\Db;
use app\admin\model\Custom as CustomModel;

class Custom extends Adminbase
{
    public $type = '';
    public function __construct()
    {
        parent::__construct();
        $this->type = $this->request->param('type');

    }
    public function index()
    {
        if ($this->request->isAjax()) {
            $where = [];
            $page = $this->request->param('page');
            $limit = $this->request->param('limit');
            if($this->request->param('filter')){
                $filter = $this->request->param('filter');
                $filter = json_decode($filter,true);
                foreach($filter as $k=>$row){
                    $where[] = [$k,'like','%'.trim($row).'%'];
                }
            }
            $offset = ($page - 1)*$limit;
            $result = CustomModel::where(['type'=>$this->type,'deleted'=>0])->where($where)->limit($offset,$limit)->select();
            $arr = [
                'text'=>'文本',
                'number'=>'数字',
                'time'=>'时间',
                'files'=>'附件',
                'radio'=>'单选框',
                'checkbox'=>'复选框',
            ];
            foreach($result as $k=>$row){
                $result[$k]['admin'] = AdminUser::getAdmin($row['admin_id'],'nickname');
                $result[$k]['field'] = $arr[$row['field_type']];
            }
            $count = CustomModel::where(['type'=>$this->type,'deleted'=>0])->where($where)->count();
            $result = array("code" => 0, 'count'=>$count,"data" => $result);
            return json($result);
        }
        $this->assign('type',$this->type);
        return $this->fetch();
    }

    public function add()
    {
        $this->assign('type',$this->type);
        return $this->fetch();
    }
    public function edit()
    {
        $id = $this->request->param('id');
        $custom = CustomModel::where('deleted',0)->find($id);
        $customDetail = Db::name('gd_custom_'.$custom['field_type'])->where('custom_id',$custom['id'])->find();
        // $customDetail['attribute'] = explode(',',$customDetail['attribute']);
        // dg($customDetail);
        if($custom['field_type'] == 'radio' || $custom['field_type'] == 'checkbox'){
            $customDetail['select'] = explode(',',$customDetail['select']);
        }
        $customDetail['hidden'] = html_entity_decode($customDetail['hidden']);
        // dg($customDetail);
        $this->assign('type',$this->type);
        $this->assign('custom',$custom);
        $this->assign('customDetail',$customDetail);
        return $this->fetch();
    }

    public function editPost()
    {
        $data = $this->request->param();
        if(isset($data['id'])){
            $customId = $data['id'];
            $customData = [
                'title'=>$data['title'],
                'mdtime'=>date('Y-m-d H:i:s')
            ];
            CustomModel::where('id',$customId)->update($customData);
            $this->handleAttr($data,$customId,1);
        }else{
            $customData = [
                'type'=>$data['type'],
                'admin_id'=>session('admin_user_auth')['uid'],
                'title'=>$data['title'],
                'field_type'=>$data['field_type'],
                'addtime'=>date('Y-m-d H:i:s')
            ];
            $customId = CustomModel::insertGetId($customData);
            $this->handleAttr($data,$customId);
        }
        $this->success('编辑成功');
    }

    public function handleAttr($data,$customId,$edit=0)
    {
        $editData = [];
        switch($data['field_type']){
            case 'text':
                $editData = [
                    'custom_id'=>$customId,
                    'is_scan'=>$data['is_scan'],
                    'can_edit_scan'=>$data['can_edit_scan'],
                    'attribute'=>$data['attribute_text']?implode(',',$data['attribute_text']):'',
                    'placeholder'=>$data['placeholder'],
                    'tips'=>$data['tips'],
                    'hidden'=>$data['hidden'],
                ];
                break;
            case 'number':
                $editData = [
                    'custom_id'=>$customId,
                    'xiaoshu'=>$data['xiaoshu'],
                    'attribute'=>$data['attribute_number']?implode(',',$data['attribute_number']):'',
                    'placeholder'=>$data['placeholder'],
                    'tips'=>$data['tips'],
                    'hidden'=>$data['hidden'],
                ];
                break;
            case 'time':
                $editData = [
                    'custom_id'=>$customId,
                    'attribute'=>$data['attribute_time']?implode(',',$data['attribute_time']):'',
                    'hidden'=>$data['hidden'],
                ];
                break;
            case 'files':
                $editData = [
                    'custom_id'=>$customId,
                    'hidden'=>$data['hidden'],
                ];
                break;
            case 'radio':
                $attribute_radio = '';
                if(isset($data['attribute_radio'])){
                    $attribute_radio = $data['attribute_radio'];
                }
                $editData = [
                    'custom_id'=>$customId,
                    'hidden'=>$data['hidden'],
                    'attribute'=>$attribute_radio?implode(',',$data['attribute_radio']):'',
                    'select'=>implode(',',$data['values']),
                    'placeholder'=>$data['placeholder'],
                    'tips'=>$data['tips'],
                    'hidden'=>$data['hidden'],
                ];
                break;
            case 'checkbox':
                $editData = [
                    'custom_id'=>$customId,
                    'hidden'=>$data['hidden'],
                    'attribute'=>$data['attribute_checkbox']?implode(',',$data['attribute_checkbox']):'',
                    'select'=>implode(',',$data['checkbox-values']),
                    'placeholder'=>$data['placeholder'],
                    'tips'=>$data['tips'],
                    'hidden'=>$data['hidden'],
                ];
                break;
        }
        if($edit == 1){
            $editData['mdtime'] = date('Y-m-d H:i:s');
            Db::name('gd_custom_'.$data['field_type'])->where('custom_id',$customId)->update($editData);
        }else{
            $editData['addtime'] = date('Y-m-d H:i:s');
            Db::name('gd_custom_'.$data['field_type'])->insert($editData);
            
        }
    }

    public function delete()
    {
        $data = $this->request->param();
        $custom = CustomModel::where('id',$data['id'])->find();
        CustomModel::where('id',$data['id'])->update(['deleted'=>1]);
        Db::name('gd_custom_'.$custom['field_type'])->where('custom_id',$data['id'])->update(['deleted'=>1]);
        $this->success('删除成功',url('index'));
    }

    public function hide()
    {
        $type = $this->request->get('type');
        $field = getProductField();
        // dg($field);
        $this->assign('field',$field);
        return $this->fetch();
    }
}